package com.fanghaoran.view.user;

import com.fanghaoran.controller.InfoController;
import com.fanghaoran.controller.StudyInfoController;
import com.fanghaoran.entity.Users;
import com.fanghaoran.view.LoginFrame;
import com.fanghaoran.view.user.bookFrame.BookCoverPanel;
import com.fanghaoran.view.user.infoFrame.InfoViewPanel;
import com.fanghaoran.view.user.notebookFrame.NoteBookPanel;
import com.fanghaoran.view.user.studyPlanFrame.StudyPlanPanel;
import com.fanghaoran.view.user.viewFrame.ViewOthersPanel;


import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * @Author: win11
 * @Date: 2023/03/18/9:43
 * @Description: 单词书的首页信息
 */
public class MainPageFrame extends JFrame {
    private Date beginTime;
    private Date endTime;

    /**
     * 用户的个人信息
     */
    private final Users user=(Users)new InfoController().getInfo(LoginFrame.usernameInput).getMessage();

    /**
     * 获取已学单词数
     * 开启页面时获取已学单词数,关闭页面时修改已学单词数并写入数据库
     */


    public MainPageFrame() throws Exception {

        //最外层界面frame的设置
        initFrame();
        //界面中的视图,控件等的设置
        initView();

        //用户一登录就执行的操作
        //打开界面就开始计算学习时间
        //关闭界面->1.统计学习时间并写入数据库 2.统计学习的单词数并写入数据库
        openOrCloseEvent();

        //设置可见
        this.setVisible(true);

        //公告
        new NoticeFrame();

    }

    private void initFrame() throws IOException {
        //设置宽高
        this.setSize(1000, 600);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置标题
        this.setTitle("扇贝单词");
        //设置标题旁边的logo图标
        this.setIconImage(ImageIO.read(new File("src//image//logo.jpg")));
        //设置置顶
        this.setAlwaysOnTop(true);
        //设置关闭模式
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void initView(){

        //菜单
        //菜单栏
        JMenuBar jmb = new JMenuBar();
        //菜单选项
        JMenu set = new JMenu("设置");
        //菜单选项下的条目
        JMenuItem switchAccount = new JMenuItem("切换账号");
        //条目添加到选项
        set.add(switchAccount);
        //选项添加到菜单栏
        jmb.add(set);
        //菜单栏添加到frame中
        this.setJMenuBar(jmb);
        //给菜单部分设置点击事件
        //切换账号
        switchAccount.addActionListener((e) -> {
            //切换账号也相当于关闭界面,要复用关闭界面要执行的代码
                    //1.统计学习时间并写入数据库
                    endTime = new Date();
                    try {
                        new StudyInfoController().updateStudyTime(beginTime, endTime,user.getId());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    //2.更新已学单词数并写入数据库
                    try {
                        new StudyInfoController().updateWordAmount(user.getId());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                    //关闭当前页面并跳转到登录页面
                    this.dispose();
                    try {
                        new LoginFrame();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
        );
        //主体内容
        //设置分隔面板
        JSplitPane sp = new JSplitPane();
        //设置支持连续布局
        sp.setContinuousLayout(true);
        //设置分割线位置
        sp.setDividerLocation(150);
        //设置分割线大小
        sp.setDividerSize(5);

        //设置左边内容(树)
        //设置根节点
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("总览");
        //设置子节点
        DefaultMutableTreeNode book = new DefaultMutableTreeNode("单词书");
        DefaultMutableTreeNode info = new DefaultMutableTreeNode("个人信息");
        DefaultMutableTreeNode plan = new DefaultMutableTreeNode("学习计划");
        DefaultMutableTreeNode view=new DefaultMutableTreeNode("查看别人");
        DefaultMutableTreeNode noteBook=new DefaultMutableTreeNode("查看生词本");

        //给根节点添加子节点
        root.add(book);
        root.add(info);
        root.add(plan);
        root.add(view);
        root.add(noteBook);
        //设置树对象并把根节点放入
        JTree tree = new JTree(root);
        //设置树对象默认选择单词书
        tree.setSelectionRow(1);
        sp.setRightComponent(new BookCoverPanel());
        sp.setDividerLocation(150);
        //给tree绑定监听,当条目选择变化时,执行方法
        tree.addTreeSelectionListener(e -> {
                    Object lastPathComponent = e.getNewLeadSelectionPath().getLastPathComponent();
                    //点击单词书的操作
                    if (book.equals(lastPathComponent)) {
                        sp.setRightComponent(new BookCoverPanel());
                        sp.setDividerLocation(150);
                    }
                    //点击个人信息的操作
                    else if (info.equals(lastPathComponent)) {
                        try {
                            sp.setRightComponent(new InfoViewPanel());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                    //点击学习计划的操作
                    else if (plan.equals(lastPathComponent)) {
                        try {
                            sp.setRightComponent(new StudyPlanPanel());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        sp.setDividerLocation(150);
                    }
                    //点击查看别人
                    else if (view.equals(lastPathComponent)) {
                        try {
                            sp.setRightComponent(new ViewOthersPanel());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        sp.setDividerLocation(150);
                    }
                    //点击查看生词本
                    else if (noteBook.equals(lastPathComponent)) {
                        try {
                            sp.setRightComponent(new NoteBookPanel());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                        sp.setDividerLocation(150);
                    }
                }
        );

        //把树放到分隔面板的左边
        sp.setLeftComponent(tree);
        //添加到frame
        this.add(sp);
    }

    public void openOrCloseEvent() {
        this.addWindowListener(new WindowAdapter() {
            /**
             *打开页面->记录开始学习的时间
             */
            @Override
            public void windowOpened(WindowEvent e) {
                super.windowOpened(e);
                beginTime = new Date();
            }

            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                //1.统计学习时间并写入数据库
                endTime = new Date();
                try {
                    new StudyInfoController().updateStudyTime(beginTime, endTime, user.getId());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                //2.更新已学单词数并写入数据库
                try {
                    new StudyInfoController().updateWordAmount(user.getId());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}
